package zsc.ruanc.practicaltraining.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.spring.web.json.Json;
import zsc.ruanc.practicaltraining.mapper.UserMapper;
import zsc.ruanc.practicaltraining.model.result.JsonResult;
import zsc.ruanc.practicaltraining.model.vo.LoginVo;
import zsc.ruanc.practicaltraining.model.vo.TokenVo;
import zsc.ruanc.practicaltraining.service.ArticleService;
import zsc.ruanc.practicaltraining.service.UserService;
import zsc.ruanc.practicaltraining.utils.IPUtils;
import zsc.ruanc.practicaltraining.utils.ResultUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "登录 | 登出 | 刷新token | IP查询")
@Validated
public class LoginoutController {

    @Autowired
    private UserService userService;

    @Autowired
    private ArticleService articleService;


    /**
     * 登录
     * @param loginVo
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录", httpMethod = "POST")
    public JsonResult login(@RequestBody LoginVo loginVo) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", userService.login(loginVo.getEmail(), loginVo.getPassword()));
        return ResultUtils.success(map);
    }

    @GetMapping("/updateToken")
    @ApiOperation(value = "刷新token", httpMethod = "GET")
    public JsonResult updateToken(HttpServletRequest httpServletRequest) {
        int id = (int)httpServletRequest.getAttribute("id");
        Map<String, Object> map = new HashMap<>();
        map.put("token", userService.refreshToken(id));
        return ResultUtils.success(map);
    }

    @PostMapping("/logout")
    @ApiOperation(value = "退出", httpMethod = "POST")
    public JsonResult logout(HttpServletRequest httpServletRequest) {
        int id = (int)httpServletRequest.getAttribute("id");
        userService.logout(id);
        return ResultUtils.success(null, "退出成功");
    }

    @GetMapping("/address")
    @ApiOperation(value = "ip转地址(管理员)", httpMethod = "GET")
    public JsonResult address(@NotBlank(message = "ip不能为空") @RequestParam(value = "ip", required = false) String ip) {
        String address = IPUtils.address(ip);
        Map map = new HashMap();
        map.put("address", address);
        return ResultUtils.success(map);
    }


    /**
     *
     * 文件上传和图片上传本在文章操作模块，但因为投诉模块也需要上传图片文件
     * 所以把接口移至这里，但逻辑代码还在ArticleService
     * 虽然不符合规则，但懒得改
     *
     */
    @PostMapping("/uploadImg")
    @ApiOperation(value = "上传图片", httpMethod = "POST")
    public JsonResult uploadImg(MultipartFile e, HttpServletRequest httpServletRequest) {
        Map map = new HashMap();
        map.put("url", articleService.uploadImg(e, (int)httpServletRequest.getAttribute("id")));
        return ResultUtils.success(map, "上传成功");
    }

    @PostMapping("/uploadFile")
    @ApiOperation(value = "上传附件", httpMethod = "POST")
    public JsonResult uploadFile(MultipartFile e, HttpServletRequest httpServletRequest) {
        Map map = new HashMap();
        map.put("url", articleService.uploadFile(e, (int)httpServletRequest.getAttribute("id")));
        return ResultUtils.success(map, "上传成功");
    }


}


